package utils.path.support;

import java.net.URL;

import utils.path.ConfigPath;
import utils.test.Assert;

/**
 * 类方式加载路径
 * 
 * @author Administrator
 *
 */
public class ClassConfigPath extends ConfigPath {
	
	public ClassConfigPath(String[] locations) {
		super(locations);
	}
	
	public ClassConfigPath(String locations) {
		super(locations);
	}

	/**
	 * 取得相对类的根路径
	 * @return
	 */
	private String getRootPath(){
		Class clz = this.getClass();
		URL url = clz.getResource("/");
		String urlName = url.getPath().substring(1);
		String pkgName = clz.getPackage().getName()+"/";
		pkgName = pkgName.replaceAll("\\.", "/");
		urlName = urlName.replaceAll(pkgName+"$", "");
		return urlName;
	}

	/**
	 * 处理配置路径
	 */
	@Override
	public String[] processConfigPath() {
		Assert.isNotNull(this.locations, "配置路径不可为空");
		Assert.hasLength(this.locations, "配置路径不可为空");
		String rootPath = getRootPath();
		for (int i=0;i<this.locations.length;i++) {
			locations[i] = rootPath + locations[i];
		}
		return this.locations;
	}
	
	public static void main(String[] args) {
		ConfigPath classPath = new ClassConfigPath("aaa,bbb");
		String[] locations = classPath.getLocation();
		System.out.println();
	}
}
